package Array.study;

import java.io.*;

public class Array {
    public static void main(String[] args) throws IOException {
        int chessArr[][] = new int[11][11];
        chessArr[1][2] = 1;
        chessArr[2][3] = 2;
        for (int[] row : chessArr) {
            for (int data : row) {
                System.out.print("\t" + data);
            }
            System.out.println();
        }


        int count = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr[i][j] != 0) {
                    count++;
                }
            }
            }
            int chessArr2[][] = new int[count + 1][3];
            chessArr2[0][0] = 11;
            chessArr2[0][1] = 11;
            chessArr2[0][2] = count;

            int sum = 0;
            for (int i = 0; i < 11; i++) {
                for (int j = 0; j < 11; j++) {
                    if (chessArr[i][j] != 0) {
                        sum++;
                        chessArr2[sum][0] = i;
                        chessArr2[sum][1] = j;
                        chessArr2[sum][2] = chessArr[i][j];
                    }
                }
            }

        File file=new File("d:\\aa.text");
        FileOutputStream fileOutputStream=new FileOutputStream(file);
        try {
            for (int i = 0; i < chessArr2.length; i++) {
                for (int j = 0; j <chessArr2[0].length ; j++) {

                    if (j==2) {
                        fileOutputStream.write(String.valueOf(chessArr2[i][j]).getBytes());
                    } else {
                        fileOutputStream.write((String.valueOf(chessArr2[i][j])+",").getBytes());
                    }
                }
                fileOutputStream.write("\n".getBytes());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            fileOutputStream.close();
        }

        for (int[] d:chessArr2
             ) {
            for (int dtaa:d
                 ) {
                System.out.print("\t"+dtaa);
            }
            System.out.println();
        }


        int chessArr3[][]=new int[chessArr2[0][0]][chessArr2[0][1]];
        for (int i = 1; i < chessArr2.length; i++) {
                chessArr3[chessArr2[i][0]][chessArr2[i][1]]=chessArr2[i][2];
        }
        for (int[] row : chessArr3) {
            for (int data : row) {
                System.out.print("\t" + data);
            }
            System.out.println();
        }
    }
}
